package com.ibeeking.gmc.global.g.rest.controller.v1;


import com.ibeeking.gmc.global.g.rest.service.IDatabaseTemplateService;
import com.ibeeking.gmc.global.service.common.param.IndustryFrontendParam;
import com.ibeeking.gmc.global.service.common.vo.*;
import com.ibeeking.gmc.global.service.common.param.DatabaseTemplateParam;
import com.ibeeking.gmc.global.service.common.query.DatabaseTemplatePageQuery;
import com.ibeeking.gmc.global.service.common.query.DatabaseTemplateQuery;
import com.ibeeking.nematos.constants.enums.OperateLogTypeEnum;
import com.ibeeking.nematos.log.annotation.Log;
import com.ibeeking.nematos.utils.base.BaseValidationGroup;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ibeeking.nematos.utils.data.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName TemplateDatabaseController控制器
 * @Description
 * @Author ibeeking
 * @Date 2021-03-02 09:07
 **/
@RestController
@RequestMapping("/g/databaseTemplate/v1")
public class DatabaseTemplateController {

    @Resource
    private IDatabaseTemplateService templateDatabaseServiceImpl;

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "有效行业列表")
    @PostMapping(value = "/industryTreeList")
    public List<DatabaseTemplateIndustryTreeVO> templateDatabaseIndustryTreeList() {
        return templateDatabaseServiceImpl.queryTemplateDatabaseIndustryTreeList();
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "有效行业终端列表")
    @PostMapping(value = "/industryFrontendTreeList/{industry}")
    public List<IndustryFrontendTreeVO> industryFrontendTreeList(@PathVariable("industry") String industry) {
        return templateDatabaseServiceImpl.queryIndustryFrontendTreeList(industry);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "有效数据库列表")
    @PostMapping(value = "/list")
    public List<DatabaseTemplateVO> list(@RequestBody DatabaseTemplateQuery query) {
        return templateDatabaseServiceImpl.list(query);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "数据库模板分页查询")
    @PostMapping(value = "/pageList")
    public Page<DatabaseTemplatePageVO> pageList(@RequestBody DatabaseTemplatePageQuery query) {
        return templateDatabaseServiceImpl.pageList(query);
    }

    @Log(logType = OperateLogTypeEnum.SAVE, describe = "数据库模板新增")
    @PostMapping(value = "/add")
    public Boolean add(@RequestBody @Validated(BaseValidationGroup.Insert.class) DatabaseTemplateParam databaseTemplateParam) {
        return templateDatabaseServiceImpl.add(databaseTemplateParam);
    }

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "数据库模板修改")
    @PostMapping(value = "/modify")
    public Boolean modify(@RequestBody @Validated(BaseValidationGroup.Update.class) DatabaseTemplateParam databaseTemplateParam) {
        return templateDatabaseServiceImpl.modify(databaseTemplateParam);
    }

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "数据库模板复制")
    @PostMapping(value = "/copyFromIndustry/{from}/{to}")
    public Boolean copyFromIndustry(@PathVariable("from") String from, @PathVariable("to") String to) {
        return templateDatabaseServiceImpl.copyFromIndustry(from, to);
    }

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "清除行业所有数据库模板")
    @PostMapping(value = "/clearTemplateDatabase/{industry}")
    public Boolean clearTemplateDatabase(@PathVariable("industry") String industry) {
        return templateDatabaseServiceImpl.clearTemplateDatabase(industry);
    }

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "设置数据源")
    @PostMapping(value = "/changeDataConfig/{industry}/{dbConfig}")
    public Boolean changeDataConfig(@PathVariable("industry") String industry, @PathVariable("dbConfig") String dbConfig) {
        if(StringUtils.isNotEmpty(dbConfig)) {
            return templateDatabaseServiceImpl.changeDataConfig(industry, Long.valueOf(dbConfig));
        }
        return false;
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "数据库模板查看")
    @GetMapping(value = "/view/{id}")
    public DatabaseTemplateVO view(@PathVariable("id") Long id) {
        return templateDatabaseServiceImpl.view(id);
    }

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "数据库模板修改状态")
    @GetMapping(value = "/changeStatus/{id}/{status}")
    public Boolean changeStatus(@PathVariable("id") Long id, @PathVariable("status") Integer status) {
        return templateDatabaseServiceImpl.changeStatus(id, status);
    }

    @Log(logType = OperateLogTypeEnum.DEL, describe = "数据库模板删除")
    @GetMapping(value = "/del/{id}")
    public Boolean del(@PathVariable("id") Long id) {
        return templateDatabaseServiceImpl.del(id);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "数据库模板行业")
    @GetMapping(value = "/industry")
    public List<DatabaseTemplateIndustryVO> templateDatabaseIndustry() {
        return templateDatabaseServiceImpl.templateDatabaseIndustry();
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "行业支持终端列表")
    @GetMapping(value = "/viewIndustryFrontend/{industry}")
    public IndustryFrontendVO viewIndustryFrontendList(@PathVariable("industry") String industry) {
        return templateDatabaseServiceImpl.viewIndustryFrontendVisible(industry);
    }

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "数据库模板修改")
    @PostMapping(value = "/modifyIndustryFrontend")
    public Boolean modify(@RequestBody @Validated(BaseValidationGroup.Update.class) IndustryFrontendParam param) {
        return templateDatabaseServiceImpl.modifyAndAddIndustryFrontend(param);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "终端列表")
    @GetMapping(value = "/frontendTypeList")
    public List<FrontendTypeVO> frontendTypeList() {
        return templateDatabaseServiceImpl.frontEndType();
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "数据库模板所属应用")
    @GetMapping(value = "/application")
    public List<ApplicationVO> application() {
        return templateDatabaseServiceImpl.application();
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "数据库模板可用所属应用")
    @GetMapping(value = "/usableApplication/{industry}")
    public List<ApplicationVO> usableApplication(@PathVariable("industry") String industry) {
        return templateDatabaseServiceImpl.usableApplication(industry);
    }
}
